package model;

import utils.Images;

import java.awt.*;

/**
 * 骷髅对象，会在柱子间随机生成
 */
public class Skeleton extends MovingObject implements Collision, Score {
    /**
     * index  动画帧下标
     * size 骷髅尺寸
     */
    int index = 1;
    int size = 60;


    Skeleton(Column column) {
        super(column.x, column.y, Images.skeleton.getWidth(null), Images.skeleton.getHeight(null));
    }

    /**
     * 判断是否和小鸟碰撞
     *
     * @param bird 小鸟对象
     * @return
     */
    @Override
    public boolean hit(Bird bird) {
        if (bird.x > x - (size + bird.size) / 2 && bird.x < x + (size + bird.size) / 2) {
            return bird.y < y + (height + bird.size) / 2 && bird.y > y - (height + bird.size) / 2;
        } else {
            return false;
        }
    }

    /**
     * 判断是否和火球碰撞
     *
     * @param fireball 火球对象
     * @return
     */
    public boolean attack(Fireball fireball) {
        if (fireball.y < y + height / 2 + fireball.size / 2 && fireball.y > y - height / 2 - fireball.size / 2) {
            return fireball.x > x;
        } else {
            return false;
        }
    }

    /**
     * 和柱子同步移动，也随柱子移出画面时重置
     */
    @Override
    public void step() {
        if (x - width / 2 < -width) {
            x = -width;
        } else {
            x -= 4;
        }
    }

    /**
     * 切换骷髅对象的动画
     */
    public void move() {
        //对index的值进行约束，防止index的值发生溢出
        if (index == 108) {
            index = 1;
        }
        Images.skeleton = Images.skeletonImages[(index / 6) % 18];
        index++;
    }


    @Override
    public int getScore() {
        return 5;
    }
}
